package com.hzqy.web.ac;

import com.hzqy.service.user_center.UserCenterServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user_center")
public class UserCenterAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	UserCenterServiceBean userCenterServiceBean;
	
	/**
	 * 获取用户积分
	 * 增值业务（音乐）中，用户定购产品包当月会送积分，但要在第二个月生效，所以第二个月需要将积分状态变更为新增，第三个月变更为可用，
	 * 实际用户可用积分为新增状态+可用状态
	 * 
	 * 当用户为0积分时，定购了一个产品包15元，第一个月为：
	 * 当月新增为0，当期调整为150，可用积分为0
	 * 第二个月为：
	 * 当月新增为150，当期调整为0，可用积分为150
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectIntegral")
	@ResponseBody
	public Object selectIntegral(HttpSession session){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userCenterServiceBean.updateIntegralAdd();
		userCenterServiceBean.updateIntegralAvailable();
		List<UserIntegralVo> userIntegralList=userCenterServiceBean.selectIntegralByServerId(serverId);
		return userIntegralList;
	}
	
	@RequestMapping("/selectUserBillByServerIdCounts")
	@ResponseBody
	public Object selectUserBillByServerIdCounts(HttpSession session,UserBillVo userBillVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBillVo.setF_ub_serverid(serverId);
		int count =userCenterServiceBean.selectUserBillByServerIdCounts(userBillVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("count", count);
		return map;
	}
	/**
	 * 获取当前用户定购的历史记录
	 * @param session
	 * @param userBillVo
	 * @return
	 */
	@RequestMapping("/selectUserBillByServerId")
	@ResponseBody
	public Object selectUserBillByServerId(HttpSession session,UserBillVo userBillVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBillVo.setF_ub_serverid(serverId);
		int count =userCenterServiceBean.selectUserBillByServerIdCounts(userBillVo);
		userBillVo.setTotalSize(count);
		List<UserBillVo> userBillList=userCenterServiceBean.selectUserBillByServerId(userBillVo);
		return userBillList;
	}
	
	/**
	 * 获取用户收藏的内容总量
	 * 必须入参为：f_cc_type 内容类型"1"电视剧"2"电影"3"直播"4"图文"5"链接"7"转码视频"20"组织模板"100"其他
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectUserBoxByServerIdCount")
	@ResponseBody
	public Object selectUserBoxByServerIdCount(HttpSession session,UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		int count =userCenterServiceBean.selectUserBoxByServerIdCount(userBoxVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("count", count);
		return map;
	}
	
	/**
	 * 获取用户收藏的内容
	 * 必须入参为：f_cc_type 内容类型"1"电视剧"2"电影"3"直播"4"图文"5"链接"7"转码视频"20"组织模板"100"其他
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectUserBoxByServerId")
	@ResponseBody
	public Object selectUserBoxByServerId(HttpSession session, UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		List<CmContentVo> contentList=userCenterServiceBean.selectUserBoxByServerId(userBoxVo);
		if(contentList == null)
			contentList = new ArrayList<CmContentVo>();
		return contentList;
	}

	/**
	 * 获取用户收藏的歌单总量
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectUserBoxCatalogCountByServerid")
	@ResponseBody
	public Object selectUserBoxCatalogCountByServerid(HttpSession session,UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		int count =userCenterServiceBean.selectUserBoxCatalogCountByServerid(userBoxVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("count", count);
		return map;
	}

	/**
	 * 获取用户收藏的歌单
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectUserBoxCatalogInfoByServerid")
	@ResponseBody
	public Object selectUserBoxCatalogInfoByServerid(HttpSession session, UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		List<CmCatalogVo> catalogList=userCenterServiceBean.selectUserBoxCatalogInfoByServerid(userBoxVo);
		if(catalogList == null)
			catalogList = new ArrayList<CmCatalogVo>();
		return catalogList;
	}
	
	/**
	 * 插入用户收藏的内容
	 * @param session
	 * @return
	 */
	@RequestMapping("/insertUserBox")
	@ResponseBody
	public Object insertUserBox(HttpSession session,UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		int result = userCenterServiceBean.insertUserBox(userBoxVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("result", result);
		return map;
	}
	
	/**
	 * 删除用户收藏的内容
	 * @param session
	 * @return
	 */
	@RequestMapping("/deleteUserBoxById")
	@ResponseBody
	public Object deleteUserBoxById(HttpSession session,UserBoxVo userBoxVo){
		String serverId = String.valueOf(session.getAttribute(SessionKey.SESSION_SERVERID));
		userBoxVo.setF_ubox_serverid(serverId);
		int result = userCenterServiceBean.deleteUserBoxById(userBoxVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("result", result);
		return map;
	}
	
	@RequestMapping("/insertIntegral")
	@ResponseBody
	public Object insertIntegral(UserIntegralVo userIntegralVo){
		int result = userCenterServiceBean.insertIntegral(userIntegralVo);
		Map<String, Integer> map=new HashMap<String, Integer>();
		map.put("result", result);
		return map; 
	}
}
